<?php
/**
 * Created by PhpStorm.
 * Script Name: BotForward.php
 * Create: 2023/2/13 15:53
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;
use plugin\wechat\app\model\BotForward as ForwardM;

class BotForward extends Common
{

    /**
     * 获取转播数据
     * @param array $params
     * @return array|mixed|\think\db\Query|\think\Model|null
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getGather($params = []){
        $group_wxid = $params['group_wxid'];
        $bot_wxid = $params['bot_wxid'];
        $from_wxid = $params['from_wxid'];
        $refresh = isset($params['refresh']) ? $params['refresh'] : 0;
        unset($params['refresh']);

        $cache_key = wechat_cache_key(serialize($params));
        $data = cache($cache_key);
        if(empty($data) || $refresh){
            $where = [
                ['f.status' ,'=', 1],
                ['f.officer','=', $from_wxid],
                ['bot.uin' ,'=', $bot_wxid]
            ];
            if($group_wxid){
                $where[] = ['g.wxid', '=', $group_wxid];
            }else{
                $where[] = ['f.group_id', '=', 0];
            }
            $data = ForwardM::alias('f')
                ->join('wechat_bot bot', 'bot.id=f.bot_id')
                ->join('wechat_bot_member g', 'g.id=f.group_id', 'left')
                ->where($where)
                ->field(['f.*'])
                ->find();
        }
        cache($cache_key, $data);
        return $data;
    }

}